package com.study.guligateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * @Author yangjin
 * @Description
 * @Date 2021/9/15/22:39
 **/
@Configuration
public class GuliMallCorsConfiguration{

    @Bean
    //springboot自带的跨域配置类
    public CorsWebFilter corsWebFilter(){
        //CorsConfigurationSourcede 的实现类 UrlBasedCorsConfigurationSource
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();

        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedHeader("*");//允许跨域携带的请求头
        corsConfiguration.addAllowedMethod("*");//允许跨域请求方法
        corsConfiguration.addAllowedOrigin("*");//允许跨域的请求源
        corsConfiguration.setAllowCredentials(true);//是否允许携带cookie进行跨域

        //registerCorsConfiguration(path,CorsConfiguration)
        source.registerCorsConfiguration("/**",corsConfiguration);

        //传入的是CorsConfigurationSource（接口）
        return new CorsWebFilter(source);
    }
}
